package com.example.demoplus.ui.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.demoplus.Constants;
import com.example.demoplus.JsonParse;
import com.example.demoplus.MainActivity;
import com.example.demoplus.R;
import com.example.demoplus.SettingsActivity;
import com.example.demoplus.adapter.ShopAdapter;
import com.example.demoplus.bean.ShopBean;
import com.example.demoplus.ui.order.SettingFragment;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HomeFragment extends Fragment implements View.OnClickListener {
    private RelativeLayout rlTitleBar;
    private TextView tvTitle;
    private ListView lvShopList;
    private ShopAdapter adapter;
    private ImageView ivSetting;
//    指定Fragment对应的布局
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        调用找到控件方法
        initView(view);
//        调用传递数据的方法
        initData();
    }

    //    寻找控件方法
    @SuppressLint("ResourceAsColor")
    private void initView(View view) {
        rlTitleBar = view.findViewById(R.id.rl_title_bar);
        tvTitle = view.findViewById(R.id.tv_title);
        lvShopList = view.findViewById(R.id.lv_shop_list);
        ivSetting = view.findViewById(R.id.iv_setting);
//        设置设置按钮可见
        ivSetting.setVisibility(View.VISIBLE);
//        给设置按钮做监听
        ivSetting.setOnClickListener(this);
//        将上下文对象传递给adapter
        adapter = new ShopAdapter(getContext());
//        放入数据
        lvShopList.setAdapter(adapter);
        tvTitle.setText("店铺");
//        为标题栏设置颜色
//        rlTitleBar.setBackgroundColor(R.color.blue_color);
    }

    //         传递数据方法
    private void initData() {
//        添加数据，数据从网络上来，需要做网络请求
        OkHttpClient client = new OkHttpClient();
//        发起请求
        Request request = new Request.Builder().url(Constants.WEB_SITE+ Constants.REQUEST_SHOP_DATA).build();
//        查看地址是否获取地址正确
//        Log.d("twm","request"+request);
        Call call = client.newCall(request);
//        异步队列做请求，有请求的时候就会做回调
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(" twm", "onFailure: " + e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                成功，获取json对象转换成String
                String json = response.body().string();
//                Log.d("twm","json"+json);
//                用JsonParse解析得到ShopBean
                final List<ShopBean> list = JsonParse.getInstance().getShopList(json);
                Log.d("twm","list"+list);
                //在UI线程里更新数据
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                      将解析好的数据放入到adapter
                        adapter.setData(list);
                    }
                });
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_setting:
                toSettingFragment();
        }
    }
    //    跳转到设置页面
    private void toSettingFragment() {
//        Intent intent = new Intent(getContext(), SettingFragment.class);
//        startActivity(intent);
        //获取fragment的实例
        Fragment fragment = new SettingFragment();
        //获取Fragment的管理器
        FragmentManager fragmentManager = getChildFragmentManager();
        //开启fragment的事物,在这个对象里进行fragment的增删替换等操作。
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //跳转到fragment，第一个参数为所要替换的位置id，第二个参数是替换后的fragment
        fragmentTransaction.replace(R.id.iv_setting, fragment);
        //提交事务
        fragmentTransaction.commit();
    }

}